/*
 * ******************************************************************************
 * Copyright (c) 2018 Robert Bosch GmbH and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/index.php
 *
 *  Contributors:
 *      Robert Bosch GmbH - initial API and functionality
 * *****************************************************************************
 */
#include <stdlib.h>
#include <stdio.h>
#include "emailHTTP.hpp"

using namespace std;

extern char emailServerAddress[256];
extern char emailServerPort[64];

int handleCode(int code)
{
	int status = 0;
	switch (code)
	{       case 100: // continue but in this case accepted.
		case 200: //Accepted
			printf("200 Accepted\n");
			printf("Registration of device to Hono instance accepted.\n");
			status = 1;
			break;
		case 201: //Created
			printf("201 Created\n");
			printf("Registration of device to Hono instance is done.\n");
			status = 1;
			break;
		case 202:
			status = 1; //Email accepted
			break;
		case 203:
		case 204:
		case 205:
		case 206:
		case 207:
		case 208:
		case 209:
			status = 1;
			break;
		case 400:
			fprintf(stderr, ("400 Bad Request\n"));
			status = 0;
			break;
		case 403:
			fprintf(stderr, ("403 Forbidden\n"));
			status = 0;
			break;
		case 409:
			fprintf(stderr, ("409 Conflict. A device with this ID is registered already. \n"));
			status = 0;
			break;
		case 503:
			fprintf(stderr, ("503 Service Unavailable. You may need to create a consumer."));
			status = 0;
			break;
		case 401:
		case 402:
		case 404:
		case 405:
		case 406:
		case 407:
		case 408:
			fprintf(stderr, ("Error in Hono connection. \n"));
			status = 0;
			break;
		default:
			status = 0;
			break;
	}
	return status;
}

int sendEmail (string emailid, string dtccode ) {

        FILE *fp;
	int code;
	int status;
        char buffer[1048 * 100];

       
        sprintf(buffer, "curl -X POST -i -H \'Content-Type: application/json\' --data-binary \'{\"emailid\":\"");
        strcat(buffer, emailid.c_str());
        strcat(buffer, "\",");
        strcat(buffer, " \"dtccode\": ");
        strcat(buffer, "\"");
        strcat(buffer, dtccode.c_str());
        strcat(buffer, "\"}\'");
	strcat(buffer, " http://");
        strcat(buffer, emailServerAddress);
        strcat(buffer, ":");
        strcat(buffer, emailServerPort);
        strcat(buffer, "/dtcinfo");
	
	//To redirect pipe to prevent stdout showing all outputs generated by curl
	strcat(buffer, " 2>/dev/null"); //2>&1 would redirect to stderr, we choose to be able to parse returned code


	//Print the command that is created
	printf("Email Command=%s\n",buffer);


	//Execute the command
	fp = popen(buffer,"r");

	//Get and Parse the output
	fgets(buffer, 13, fp); //Get the string HTTP/1.1 XXX

	//Prepare the response code
	sscanf(buffer, "HTTP/1.1 %d", &code);


	//Print the code
	printf("Code=%d\n",code);

	//Print the response
	printf("Response=%s\n",buffer);


	/*if (code == 503)
        {
                pthread_exit(NULL);
        }*/
	

	//Handle the code and return status: 1-succeeded 0-failed
	status = handleCode(code);

	//Close the file
	fclose(fp);

	//Return status
	return status;
}

